<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/6 0006
 * Time: 下午 3:51
 */

namespace Admin\Controller;

use Think\Controller;
class GoodsController extends  BaseController
{
    function __construct()
    {
        parent::__construct();
        $this->goods = D('Admin/Goods');
    }
    //处理获取属性的AJAX请求
    public function ajaxGetAttr(){
        $typeId =I('get.type_id');
        $attrModel = D('Attribute');
        $attrData = $attrModel->where(
            array('type_id'=>array('eq',$typeId))
        )->select();
        echo json_encode($attrData);
    }

    public function add(){

        //取出所有品牌
        $brand = D('Brand');
        $brandData = $brand -> select();
        //取出所有会员级别
        $mlMode = D('MemberLevel');
        $mlData = $mlMode ->select();
        //取出所有分类
        $cateModel = D('Category');
        $cateData = $cateModel->getTree();

        if(IS_POST){
            if($this->goods->create(I('post.'))){
                $this->goods->add();
                $this->success("添加成功",U('Goods/goodsList'));
            }else{
                $this->error(($this->goods->getError()));
            }

        }else{
            $this->assign(array(
                'cateData'=>$cateData,
                'MemberLevel'=>$mlData,
                'brandData' =>$brandData,
                '_page_title' =>"添加商品",
                '_page_btn_name' =>'商品列表',
                '_page_btn_link'=>U('goodsList')
            ));
            $this->display();
        }
    }
    public function goodsList(){
        //取出所有分类
        $cateModel = D('Category');
        $cateData = $cateModel->getTree();
        //取出所有品牌
        $brand = D('Brand');
        $brandData = $brand -> select();
        $data = $this->goods->search();
        $this->assign('cateData',$cateData);
        $this->assign('goodsList',$data['data']);
        $this->assign('page',$data['page']);
        $this->assign(array(
            'brandData' => $brandData,
            '_page_title' =>"商品列表",
            '_page_btn_name' =>'添加商品',
            '_page_btn_link'=>U('add')
        ));
        $this->display();
    }
    public function goodsEdit(){
        if(IS_POST){ //var_dump(array_merge(I('post.'), array('id'=>I('post.goods_id')))); exit;
            if($this->goods->create(I('post.'))){
                $this->goods->where("id =".I('post.id'))->save();
                $this->success('修改成功',U('Goods/goodsList'));
            }else{
               $this->error(($this->goods->getError()));
             }
        }else{
            $id = intval(I('get.id'));
            //会员级别修改显示
            $mlModel = D('MemberLevel');
            $mlData = $mlModel->select();
            //会员价格修改显示
            $mpModel = D('member_price');
            $mpData = $mpModel->where(
                array(
                'goods_id' => array('eq',I('get.id'))
                )
                 )->select();

            $_mpData =array();
            foreach ($mpData as $k => $v){
                $_mpData[$v['level_id']] = $v['price'];
            }
            //修改品牌显示
            $brand = D('Brand');
            $brandData = $brand->select();
            //取出所有分类做下拉框
            $cateModel = D('Category');
            $cateGory  = $cateModel->select();
            //取出扩展分类ID
            $gcModel = D('goods_cat');
            $gcData = $gcModel->field('cat_id')->where(
              array("goods_id" => array('eq',I('get.id')))
            )->select();
            //取出所有分类
            $catModel = D('Category');
            $cateData = $cateModel->getTree();

            //修改显示相册
            $gpModel = D('goods_pic');
            $gpData =$gpModel->field('id,mid_pic')
                ->where(array('goods_id' => array('eq',I('get.id'))))
                ->select();
            $goods_row = $this->goods->where('id='.$id)->find();
            //商品属性值显示
            $attrModel = D('Attribute');
            $attrData = $attrModel
                ->alias('a')
                ->field('a.id attr_id,a.attr_name,a.attr_type,a.attr_option_values,b.attr_value,b.id')
                ->join('LEFT join __GOODS_ATTR__ b ON (a.id=b.attr_id AND b.goods_id ='.$id.')')
                ->where(array(
                    'a.type_id' =>array('eq',$goods_row['type_id']),
                ))->select();

            $this->assign('img',showImage($goods_row['sm_logo']));
            $this->assign("row",$goods_row);
            $this->assign(array(
                'gaData' =>$attrData,
                'gcData' => $gcData,
                'cateData'=> $cateData,
                'cateGory'=>$cateGory,
                'gpData'=>$gpData,
                'mpData' =>$_mpData,
                'mlData'    => $mlData,
                'brandData' => $brandData,
                '_page_title' =>"修改商品",
                '_page_btn_name' =>'商品列表',
                '_page_btn_link'=>U('goodsList')
            ));
            $this->display();
        }

    }
    public function good_number(){
        $id = I('get.id');
        $gnModel = D('goods_number');
        if(IS_POST){
            $gaid = I('post.goods_attr_id');
            $gn = I('post.goods_number');
            $gnModel->where(array(
                'goods_id'=>array('eq',$id)
            ))->delete();
            //先计算商品属性ID和库存量的比例
            $gaidCoutn = count($gaid);
            $gnCount = count($gn);
            $rate = $gaidCoutn/$gnCount;
            $_i =0;
            foreach ($gn as $k => $v){
                $_goodsAttrId = array();
                for($i=0; $i<$rate; $i++){
                    $_goodsAttrId[] = $gaid[$_i];
                    $_i++;
                }
                sort($_goodsAttrId,SORT_NUMERIC);
                $_goodsAttrId = (string)implode(',',$_goodsAttrId);
                $gnModel->add(array(
                    'goods_id'=> $id,
                    'goods_attr_id' =>$_goodsAttrId,
                    'goods_number' =>$v
                ));
            }
            $this->success("添加成功",U('Goods/goodsList'));
        }else{
            $gaModel = D('goods_attr');
            $gnModel = D('goods_number');
            $gnData  = $gnModel->where(array('goods_id'=>$id))->select();
            $gaData  = $gaModel->alias('a')->field('a.*,b.attr_name')
                ->join('LEFT JOIN __ATTRIBUTE__ b ON a.attr_id = b.id')
                ->where(array(
                    'goods_id'=>array('eq',$id),
                    'b.attr_type' =>array('eq','op')
                ))->select();
            $_gaData =array();
            foreach ($gaData as $k => $v){
                $_gaData[$v['attr_name']][]=$v;
            }
            $this->assign(
                array(
                    'gnData' =>$gnData,
                    'gaData' =>$_gaData,
                    '_page_title' =>"库存量",
                    '_page_btn_name' =>'返回列表',
                    '_page_btn_link'=>U("lst")
                )
            );

            $this->display('Goods/goods_number_lst');
        }
    }
    public function ajaxPicDel(){
        $picId =I('get.picid');
        $gpModel = D('goods_pic');
        $pic = $gpModel->field('pic,sm_pic,mid_pic,big_pic')->find($picId);
        deleteImage($pic);
        $gpModel->delete($picId);
    }
    //ajax删除商品属性
    public function ajaxDelAttr(){
        $goodsID = addslashes(I('get.goods_id'));
        $gaid =addslashes(I('get.gaid'));
        $gaModel = D('goods_attr');
        $gaModel->delete($gaid);
        $gmModel = D('goods_number');
        $gmModel->where(array(
            'goods_id' =>array('EXP',"=$goodsID AND FIND_IN_SET($gaid,goods_attr_id)" ),
        ))->delete();
    }

    public  function goodsDel(){
    $id =intval(I('get.id'));
    if($this->goods->delete($id)){
        $this->success('成功放入回收站',U('Goods/goodsList'));
        }else{
        $this->error(($this->goods->getError()));
        }
    }
}